# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Maintainer: Caleb Maclennan <caleb@alerque.com>

pkgbase=nix
pkgname=('nix' 'nix-docs')
pkgver=2.19.1
_commit='455aca36e4e99b418f22146614883c1ab0dc418b'
pkgrel=1
pkgdesc='A purely functional package manager'
arch=('loong64' 'x86_64')
url='https://nixos.org/nix'
license=('LGPL')
makedepends=(
  'aws-sdk-cpp'
  'aws-crt-cpp'
  'autoconf-archive'
  'boost'
  'brotli'
  'bzip2'
  'curl'
  'editline'
  'gc'
  'git'
  'graphviz'
  'gtest'
  'jq'
  'libcpuid'
  'libseccomp'
  'libsodium'
  'lowdown'
  'mdbook'
  'mdbook-linkcheck'
  'nix-busybox'
  'nlohmann-json'
  'openssl'
  'rapidcheck'
  'sqlite'
)
source=(
  "$pkgbase::git+https://github.com/NixOS/nix.git#commit=$_commit"
  'nix.conf'
  'sysusers.conf'
  'tmpfiles.conf'
  'user.tmpfiles.conf'
)
sha512sums=('SKIP'
            '46fdd1134626da81f0ec3ada47b69bb803277e8dd379052db5d8606475f5ff7ece63c8ae41b5f8ba6d2174465e1e977212c648a11c853b199e11b2014789a661'
            '6511badd6e4c71d9b7f6e6a87ea521a9727a5569ecf48dca11ee0d04151adf083453dcf7e05eadd5c28278fd8aee9fe44bbb90067f5fc8067c99c5e9665affd9'
            '879bc28faf877a560ac67500388d9774ec890391ef08d367a18f98ef17faeef8c7d208e747fed1fd501b07c3a3b2286af436eb1bd137de2d8ea9a9c32b48088a'
            'da5b6d449f8e69c9211e6bd97b1ce4c6e398da5a34fea9a8a89a28084489a1549188e52f66ce7536903afeb75e690e764188daac20a16227b66cc14a72b7311a')
b2sums=('SKIP'
        'e79ceeb12faf7c38a167857ded355a74511c1d98e7fcb1efb589072c16b35f017239e6723bad04e13ed5b16cbebc91923363e36c130bf51d06a66a4d4ef54f6b'
        'e92b8192bae89f0bc8c62371fbe63b4bfd68352699e5ea6900bf4313fd573322a06cb85074b292a005cbf644a50abbb67694f9458f071b0fd223d220494f6b6c'
        'bffaabbe8c9efee79c7761e72a189796f35c0a40805b53fe954c10590ab9bfd0a321b45c7050ddf30810e36fa4431768f36f19fdc55085c0396ac5338b88f839'
        '57610fc5fa370f9c3df50ab411dfe154b273349b3b321ad01768cacdf371e27603e8446a4e4269ccf4ca6cdb59b4a70fdcac59cda29c47eb7e7224555ec7e2e2')

pkgver() {
  cd "$pkgbase"

  git describe --tags
}

prepare() {
  cd "$pkgbase"

  sed -i "s:\$(bindir):src/nix:g" doc/manual/local.mk
}

build() {
  cd "$pkgbase"

  autoreconf -vfi

  ./configure \
    --prefix=/usr \
    --libexecdir="/usr/lib/$pkgname" \
    --sysconfdir=/etc \
    --localstatedir=/nix/var \
    --with-sandbox-shell=/usr/lib/nix/busybox \
    --enable-static=rapidcheck \
    --enable-gc \
    --enable-lto

  make
}

check() {
  cd "$pkgbase"

  make check
}

package_nix() {
  depends=(
    'glibc'
    'gcc-libs'
    'aws-sdk-cpp'
    'aws-crt-cpp'
    'boost-libs' 'libboost_context.so'
    'brotli' 'libbrotlienc.so' 'libbrotlidec.so'
    'curl' 'libcurl.so'
    'editline' 'libeditline.so'
    'gc'
    'libarchive' 'libarchive.so'
    'libcpuid' 'libcpuid.so'
    'lowdown' 'liblowdown.so'
    'libseccomp' 'libseccomp.so'
    'libsodium' 'libsodium.so'
    'nix-busybox'
    'openssl' 'libcrypto.so'
    'sqlite' 'libsqlite3.so'
  )
  backup=("etc/$pkgname/$pkgname.conf")
  install="$pkgname.install"

  # package
  cd "$pkgbase"
  make DESTDIR="$pkgdir" install

  # remove unused files - FS#73120
  rm -rf \
    "$pkgdir/etc/init" \
    "$pkgdir/etc/profile.d/nix.sh" \
    "$pkgdir/etc/profile.d/nix.fish"

  # systemd integration - FS#79747
  install -vDm644 "$srcdir/tmpfiles.conf" "$pkgdir/usr/lib/tmpfiles.d/$pkgname-daemon.conf"
  install -vDm644 "$srcdir/sysusers.conf" "$pkgdir/usr/lib/sysusers.d/$pkgname-daemon.conf"
  install -vDm644 "$srcdir/user.tmpfiles.conf" "$pkgdir/usr/share/user-tmpfiles.d/$pkgname-daemon.conf"

  # configuration
  install -vDm644 "$srcdir/nix.conf" -t "$pkgdir/etc/$pkgname"

  # move docs out for nix-docs split-package
  mv "$pkgdir/usr/share/doc" nix-docs

  # license
  install -Dm644 COPYING "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

package_nix-docs() {
  pkgdesc+=" (documentation)"

  cd "$pkgbase"
  install -vd "$pkgdir/usr/share/doc"
  mv nix-docs/nix "$pkgdir/usr/share/doc"
}
